const App = getApp();
Page({
  data: {
    year: 0,
    month: 0,
    date: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0,
    order_id: 0,
    getDate: [],
    isToday8: 0,
    datetime: 0,
    time_id: 0,
  },
  DateDuan: function (e) {
    var time_id = e.currentTarget.id
    this.setData({
      time_id: time_id
    })
    // var index = e.currentTarget.dataset.index;
    // var item = this.data.getDate[index];
    // console.log(item)
    // item.status = !item.status;
    // this.setData({
    //   getDate: this.data.getDate,
    // })
  },
  dateXuanze: function (e) {
    var isToday = e.currentTarget.dataset.date
    var today = this.data.datetime
    if (parseInt(isToday) < parseInt(today)) {
      wx.showModal({
        title: '温馨提示：',
        content: '不能选之前日期',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    var isToday8 = e.currentTarget.dataset.datetime
    this.setData({
      isToday: isToday,
      isToday8: isToday8,
      time_id:0,
    })
    this.getDate()
  },
  Submutsuccess: function () {
    let _this = this;
    var order_id = _this.data.order_id
    if (!order_id) {
      wx.showModal({
        title: '温馨提示：',
        content: '获取不到订单ID',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    var date = _this.data.isToday8
    if (!date) {
      wx.showModal({
        title: '温馨提示：',
        content: '请选择日期',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    var time_id = _this.data.time_id
    if (!time_id) {
      wx.showModal({
        title: '温馨提示：',
        content: '请选择时间段',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    App._post_form('service/confirmTime', {
      order_id: order_id,
      date: date,
      time_id: time_id
    }, function (result) {
      wx.showModal({
        title: '温馨提示：',
        content: result.msg,
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {
          wx.navigateBack();
        }
      })

    });
  },
  getDate: function () {
    let _this = this;
    var order_id = _this.data.order_id
    var date = this.data.isToday8
    App._post_form('service/confirmTimeList', {
      order_id: order_id,
      date: date
    }, function (result) {
      var getDate = result.data.list
      var time_id = 0
      if (result.data.order) {
        time_id = result.data.order.time_id
      }
      _this.setData({
        getDate: getDate,
        time_id: time_id,
        isToday:result.data.order.server_date1,
        isToday8:result.data.order.server_date,
      })
    });
  },
  onLoad: function (options) {
    var order_id = options.order_id
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    this.dateInit();
    this.setData({
      year: year,
      month: month,
   //   isToday: '' + year + month + now.getDate(),
    //  isToday8: '' + year + '-' + (month > 9 ? month : ('0' + month)) + '-' + (now.getDate() > 9 ? now.getDate() : ('0' + now.getDate())),
      order_id: order_id,
      datetime: parseInt('' + year + '' + (month > 9 ? month : ('0' + month)) + '' + (now.getDate() > 9 ? now.getDate() : ('0' + now.getDate()))),
    })
    this.getDate()
  },
  dateInit: function (setYear, setMonth) {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let dateArr = []; //需要遍历的日历数组数据
    let arrLen = 0; //dateArr的数组长度
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    let nextYear = 0;
    let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
    let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期
    let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
    let obj = {};
    let num = 0;

    if (month + 1 > 11) {
      nextYear = year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek + dayNums;
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        num = i - startWeek + 1;
        obj = {
          isToday: parseInt((year + '' + ((month + 1) > 9 ? (month + 1) : ('0' + (month + 1))) + '' + (num > 9 ? num : ('0' + num)))),
          isToday8: '' + year + '-' + ((month + 1) > 9 ? (month + 1) : ('0' + (month + 1))) + '-' + (num > 9 ? num : ('0' + num)),
          dateNum: num,
          weight: 5
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    }
    this.setData({
      dateArr: dateArr
    })

    let nowDate = new Date();
    let nowYear = nowDate.getFullYear();
    let nowMonth = nowDate.getMonth() + 1;
    let nowWeek = nowDate.getDay();
    let getYear = setYear || nowYear;
    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;

    if (nowYear == getYear && nowMonth == getMonth) {
      this.setData({
        isTodayWeek: true,
        todayIndex: nowWeek
      })
    } else {
      this.setData({
        isTodayWeek: false,
        todayIndex: -1
      })
    }
  },
  lastMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
    let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  nextMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
    let month = this.data.month > 11 ? 0 : this.data.month;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  }
})